package br.com.jvet.persistencia;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.Query;

import br.com.jvet.entidade.Exame;
@Stateless
public class ExameDAO extends AbstractDAO<Exame, Integer>{

	private static String SELECT_EXISTE= "SELECT e from "+Exame.class.getSimpleName()+" e where upper(e.descricao) =:descricao and e.tipoExame =:tipoexame";

	
	public ExameDAO() {
		super(Exame.class);
		
	}
	
	public boolean existe(Exame exame) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("descricao", exame.getDescricao().toUpperCase().trim());
		parameters.put("tipoexame", exame.getTipoExame());
		Exame outro = super.buscarUmResultado(SELECT_EXISTE,
				parameters);
		if (outro!=null && outro.getCodigo()!=exame.getCodigo()) {
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public List<Exame> buscarPorTipoExame(Exame exame) {
		System.out.println("busca tipo exame"+exame.getTipoExame().getDescricao());
		
		Query c = super
				.getEntityManager()
				.createQuery(
						"SELECT e FROM "
								+ Exame.class.getSimpleName()
								+ " e "
								+ " where e.tipoExame =:tipoexame");
		c.setParameter("tipoexame", exame.getTipoExame());
		
		return (List<Exame>)c.getResultList();
	}
	
	

}
